const tools = require("./tools");
const local = require("./forLocalItems");

function createLocalItemObject( localName , defValue , type , openLocal/*是否开启本地存储*/ ){
    openLocal = tools.selectValue( openLocal , true );
    return {
        _v : undefined,
        _type : type || "number",
        _localName : localName,
        _init(){
            if( tools.isType(this._v , "Undefined") ){
                this._v = openLocal ? local.get( this._localName ) : tools.selectValue(defValue , 0);
                if(this._v !=="0" && !this._v){
                    this._v =  tools.selectValue(defValue , 0);
                    openLocal && local.set( this._localName , this._v );
                }
                this._v = this._type=="number" ?  parseInt(this._v) : this._v;
            }
        },
        
        get(){
            this._init();
            return this._v;
        },

        add( v ){
            this._init();
            if(this._type == 'number'){
                this._v += v ;
            }else if( tools.isType(this._v , "Array") ){
                this._v.push( v );
            }
            openLocal && local.set( this._localName , this._v );
            return this._v;
        },

        push( v ){
            return this.add(v);
        },

        set( coins , index ){
            if(typeof index!="undefined"){
                this._v[index] = coins;
            }else{
                this._v = this._type=="number" ? parseInt(coins) : coins;
            }
            openLocal && local.set( this._localName , this._v );
        },

        clear(){
            console.log("clear:"+localName);
            this._v = this._type=="number" ? 0 : defValue;
            openLocal && local.set( this._localName , this._v );
        }
    }
}

//寄生于gamedataObjclass类型的 数据项
function createGameDataObject( localName  ,type , defValue , OBJ ){
    return {
            _isSubObj : 1,
            _v : undefined,
            _type : type || "number",
            //_gloabName : gloabName || "",
            _localName : localName,
            _init( flag ){
                if( Me.isType(this._v , "Undefined") || flag ){
                    this._v = OBJ ? OBJ.get( this._localName ) : Me.selectValue(defValue , 0);
                    this._v = Me.selectValue(this._v , Me.selectValue(defValue , 0)) ;
                    //if(localName=='__pop_user_hislev') {
                        //console.log("getLocal "+localName+" :")
                        ///console.log(this._v);
                    //}
                    if(!Me.isType(this._v , "Number") && this._v !=="0" && !this._v){
                        this._v =  Me.selectValue(defValue , 0);
                        OBJ && OBJ.set( this._localName , this._v );
                    }
                    this._v = this._type=="number" ?  parseInt(this._v) : this._v;
                }
            },
            get(){
                this._init();
                return this._v;
            },
            add( v , key ){
                this._init();
                if(this._type == 'number'){
                    this._v += v ;
                }else if( Me.isType(this._v , "Array") ){
                    this._v.push( v );
                }else if( Me.isType(this._v , "Object") && key ){
                    this._v[key] = v ;
                }
                OBJ && OBJ.set( this._localName , this._v );
                return this._v;
            },
            push(){
                return this.add.apply( this , arguments );
            },
            set( v , index ){
                if(typeof index!="undefined"){
                    this._v[index] = v;
                }else{
                    this._v = this._type=="number" ? parseInt(v) : v;
                }
                OBJ && OBJ.set( this._localName , this._v );
            },
            delIndexs( i ){
                for(let t=0;t<parseInt(i)-1;t++){
                    if( this._type == 'object' && this._v[t] ){
                        delete(this._v[t]);
                    }else if(this._type == 'array' && this._v[t]){
                        this._v[t] = null;
                    }
                }
                OBJ && OBJ.set( this._localName , this._v );
            },
            clear(){
                Gloab.say("clear:"+localName);
                this._v = this._type=="number" ? 0 : defValue;
                OBJ && OBJ.set( this._localName , this._v );
            },

            saveLocal(){
                OBJ.save( this._localName );
            },

            loadFromGloab(){
                this._init( true );
            }
    }
}

module.exports = {
    create : createLocalItemObject 
}; 